//
//  ViewController.m
//  上拉刷新
//
//  Created by KM on 16/10/27.
//  Copyright © 2016年 KM. All rights reserved.
//

#import "ViewController.h"
#import "ZhuceData.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *tel;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.password.secureTextEntry = YES;
    self.password.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.name.clearButtonMode = UITextFieldViewModeWhileEditing;
    
}

- (IBAction)zhuce:(id)sender
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/node/reg?name=%@&password=%@&tel=%@",self.name.text,self.password.text,self.tel.text]];
    
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *tesk = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSString *text = [[NSString alloc]initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        NSLog(@"%@",text);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        ZhuceData *dr = [[ZhuceData alloc]initWithDictionary:dict];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (dr.errcode == 0) {
                NSLog(@"注册成功.");
            }
            else
            {
                NSLog(@"%@",dr.errinfo);
            }
        });
    }];
    [tesk resume];

}

@end